package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.Sn;
import com.example.hcbmsm.domain.SnExample;
import com.example.hcbmsm.mapper.SnMapper;
import com.example.hcbmsm.service.SnService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.Map;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/30 19:09
 */
@Service
public class SnServiceImpl implements SnService {
    @Autowired
    private SnMapper snMapper;

    @Override
    public Map add(String token,@RequestBody Sn sn) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        sn.setPatId(new Long(tokenInfo.get("patId").asString()));
        if (snMapper.insertSelective(sn) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map list(String token) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long docId = new Long(tokenInfo.get("docId").asString());
        return HCBMSMResult.SUCCESS(snMapper.selectByDocId(docId));
    }

    @Override
    public Map renew(@RequestBody Sn sn) {
        sn.setModifyTime(new Date());
        if (snMapper.updateByPrimaryKeySelective(sn) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long snId) {
        if (snMapper.deleteByPrimaryKey(snId) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
